﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int n = 0;
            int result = 1;
            try
            {
                // 捕获格式异常：非数字输入
                n = int.Parse(textBox1.Text);
                // checked块：启用溢出检查（实验提示核心，默认不检查）
                checked
                {
                    for (int i = 1; i <= n; i++)
                    {
                        result *= i;
                        // 若n过大（如13以上），此处触发溢出异常
                    }
                }
                textBox2.Text = result.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("输入格式不合法！");
                textBox1.Clear();
            }
            catch (OverflowException)
            {
                MessageBox.Show("溢出错误！n值过大");
                textBox1.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show("其它异常：" + ex.Message);
                textBox1.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            textBox2.Clear();
            // 可选：将光标定位到输入框，方便用户再次输入
            textBox1.Focus();
        }
    }
}
